package com.sinai.mshab.client.util;

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.IsSerializable;
import com.sinai.mshab.client.ui.aggregate.FormData;

/**
 * Stores information about the current user.
 * @author serge
 */
public class User implements IsSerializable {
	
	private String username;
	private String permission;
	
	/**
	 * @param username the username to set
	 */
	public void setUsername(String u) {
		username = u;
	}
	
	/**
	 * @return the username
	 */
	public String getUsername() {
		return username;
	}
	
	/**
	 * @param permission the permission to set
	 */
	public void setPermission(String p) {
		permission = p;
	}
	
	/**
	 * @return the permission
	 */
	public String getPermission() {
		return permission;
	}
	
	public static String getUserPermission() {
		String cookie = Cookies.getCookie(FormData.getServletUrl() + " cookie");
		if (cookie == null) {
			return null;
		}
		return cookie;
	}

	/**
	 * Check if user can login via cookie and avoid login screen
	 * 
	 * @return true if user has a valid cookie
	 */
	public static boolean loggedIn() {
		String cookie = Cookies.getCookie(FormData.getServletUrl() + " cookie");
		if (cookie == null) {
			return false;
		}
		return true;
	}
}
